package org.hot100_6.leetcode56;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals,(a,b)->{return a[0]-b[0];});

        int start = intervals[0][0];
        int end = intervals[0][1];

        int n = intervals.length;

        List<int []> res = new ArrayList<>();

        for (int i = 1; i < n; i++) {
            if (intervals[i][0]>end){
                int [] temp = {start,end};
                res.add(temp);
                start = intervals[i][0];
                end = intervals[i][1];
            }else {
                end = Math.max(end,intervals[i][1]);
            }
        }

        int [] temp = {start,end};
        res.add(temp);

        int [][] result = new  int[res.size()][];

        for (int i = 0; i < res.size(); i++) {
            result[i]=res.get(i);
        }

        return  result;
    }
}
